var gname;

$(document).ready(function () {
    
    // 设置用户登录状态
    set_login_status();
    // 注销按钮单击事件
    $("#btn-logout").click(function (e) { 
        logout();        
    });
    get_cart();

    $("#back").click(function (e) { 
        goback();        
    });

    $("#allinall").click(function (e) { 
        finish();        
    });

    
});

function set_login_status() {
    var $A = $(".user-name");
    if (!$A) return false;

    $.ajax({
        type: "GET",
        url: SERVER_PATH + "/user/status",
        xhrFields: {withCredentials: true},
        success: function (result) {
            if (result.status == "0" && result.data) {
                $A.text(result.data.username);
                gname=result.data.username;
                $("#user-info").show();
            } else {
                $("#user-info").hide();
            }
        }
    });    
}

function logout() {
    $.ajax({
        type: "GET",
        url: SERVER_PATH + "/user/logout",
        xhrFields: {withCredentials: true},
        success: function (result) {
            if (result.status) {
                alertBox(result.data.message);
                return false;
            }
            alertBox("已经注销！", function(){
                window.location.href = "./login.html"
            });
        }
    });    
}

     
    function get_cart() {

        var un = $.getUrlParam("username");
        $("#home a").attr("href","seckill.html?username="+un);
        $.ajax({
            type: "GET",
            dataType: "json", 
            url: SERVER_PATH + "/cart/cartlist",
            data: {
                "username": un
            },
            xhrFields: {withCredentials: true},
            success: function (result) {
                if (result.status) {
                    alertBox(result.data.message);
                    return false;
                }
                set_cart(result.data);

            }
        });
    }
    
    
    
    function set_cart(items) {
        if (!items) {
            return false;
        }
    
    
    
        $.each(items, function (i, item) {
 
                    var tr=
                    '<tr class=hisclas>'
                    +'<td class="cartnm">'+'<span >1</span>' +'<td>'
                    +'<td class="cartpr">'+'<span>1000</span>' +'<td>'
                    +'<td >'+'<span class="pic"><img width="20%" src=""/></span>' +'<td>'
                    +'<td class="cartam">'+'<span>1000</span>' +'<td>'
                    +'<td class="del">'+'<a href="item.html"> <span style="cursor:pointer;" >删除</span></a>' +'<td>'
                    +'</tr>';
    
            var pro=item;
            
            var $prod = $(tr);
            $prod.find(".cartnm span").text(item.name); 
            $prod.find(".cartpr span").text(item.price);
            $prod.find(".pic img").attr("src", item.imageUrl);
            $prod.find(".cartam span").text(item.proNum);

            $prod.find(".del a").attr("href", "delcart.html?id=" + item.proId+"&username="+gname);
            

            $(".cartslist").append($prod);
           
        });
        
        console.log(gname);
         $(document).find("button").attr("href","seckill.html?username="+gname);

    }
    
    function goback(){
        console.log(gname);

         window.location.href = "seckill.html?username="+gname;

    }
    function finish(){
$.ajax({
            type: "POST",
            dataType: "json", 
            url: SERVER_PATH + "/cart/createOrder",
            data: {
                "username": gname
            },
            xhrFields: {withCredentials: true},
            success: function (result) {
                if (result.status) {
                    alertBox(result.data.message);
                    return false;
                }
                alertBox("结算成功！", function() {
                    window.location.href = "seckill.html?username="+gname;
                });

            }
        });
    }


    